/**
 *  @file JSONQuickParser.cpp
 */
#include "JSONQuickParser.h"
#include "JSONException.h"
#include "../io/FileStream.h"
#include "../io/StreamReader.h"
#include "__internal/QuickParser.h"

using namespace cppflib::io;

namespace cppflib
{

namespace json
{

JSONQuickParser::JSONQuickParser(void)
{
   this->markErrorForDupName = true; // true by default
}

/**
 *  @param [in] markErrorForDupName -- true to consider error when duplicated name is found within
 *                                     the same JSON object
 */
JSONQuickParser::JSONQuickParser(bool markErrorForDupName)
{
   this->markErrorForDupName = markErrorForDupName;
}

JSONQuickParser::~JSONQuickParser(void)
{
}


void JSONQuickParser::Parse(pcwstr_t jsonFilePath, JSONHandler &handler)
{
   FileStream fs(jsonFilePath, FileStream::FA_READ_DETECTENC);
   FileStream::fileencoding_t encoding = fs.GetInputEncoding();
   if (encoding != FileStream::ENC_UTF8 && encoding != FileStream::ENC_NONE)
      throw JSONException(_S("Unsupported file encoding"));

   StreamReader sReader(&fs, text::CS_UTF8);
   Parse(sReader, handler);
}

void JSONQuickParser::Parse(Reader &reader, JSONHandler &handler)
{
   QuickParser parser(reader, handler, this->markErrorForDupName);
   parser.StartParsing();
}

} // end of namespace json

} // end of namespace cppflib
